package com.zuser.project.controller;

import com.zuser.project.dao.CustomerInfoDao;
import com.zuser.project.entity.CustomerInfoEntity;
import com.zuser.project.service.CustomerInfoService;
import com.zuser.project.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/customer/customerinfo")
@Validated
@Tag(name = "客户信息管理", description = "客户信息管理模块的接口")
public class CustomerInfoController {
   @GetMapping("/info")
   public String test1(){
       return "test 测试这个数据内容";
   }
    @Autowired
    private CustomerInfoService customerInfoService;

    /**
     * 列表查询
     */
    @Operation(summary = "分页查询客户列表", description = "根据条件分页查询客户信息")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", example = "1"),
            @Parameter(name = "limit", description = "每页条数", example = "10"),
            @Parameter(name = "key", description = "搜索关键字", example = "小明")
    })
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        return customerInfoService.queryPage(params);
    }

    /**
     * 获取客户详情
     */
    @Operation(summary = "获取客户详情", description = "根据ID获取客户信息")
    @Parameter(name = "id", description = "客户ID", required = true, example = "1")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @GetMapping("/info/{id}")


    public R info(
            @PathVariable("id")
            @NotNull(message = "客户ID不能为空") Long id) { // 校验 ID 是否为空
        CustomerInfoEntity customerInfo = customerInfoService.getById(id);
        return R.ok().put("customerInfo", customerInfo);
    }

    /**
     * 保存客户信息
     */
    @Operation(summary = "保存客户信息", description = "新增客户信息")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping("/save")
    public R save(
            @RequestBody
            @Valid CustomerInfoEntity customerInfo) { // 校验整个实体类
        customerInfoService.save(customerInfo);
        return R.ok();
    }

    /**
     * 修改客户信息
     */
    @Operation(summary = "修改客户信息", description = "更新客户信息")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping("/update")
    public R update(
            @RequestBody
            @Valid CustomerInfoEntity customerInfo) { // 校验整个实体类
        customerInfoService.updateById(customerInfo);
        return R.ok();
    }

    /**
     * 删除客户信息
     */
    @Operation(summary = "批量删除客户信息", description = "根据ID列表删除客户信息")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    @PostMapping("/delete")
    public R delete(
            @RequestBody
            @NotNull(message = "ID列表不能为空") Long[] ids) { // 校验数组是否为空
        customerInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

   @Resource
   private CustomerInfoDao customerInfoDao;
    @Operation(summary = "动态分页查询客户信息", description = "根据条件动态分页查询客户信息")
    @PostMapping("/customer/page")
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = R.class)))
    public R queryList(@RequestBody Map<String, Object> params) {
        System.out.println(params);
        return R.ok().put("data",customerInfoDao.selectByDynamicPage(params));
    }

}